﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using Luna小店.DTO;
using mendian;
using Luna小店.model;
using Luna小店.Utils;

namespace Luna小店.UC.物料管理
{
    public partial class Frm物料分类 : DevExpress.XtraEditors.XtraForm
    {
        private List<DTO物料清单> list物料清单;
        private String catalogId;
        private DTO物料清单 DTO物料清单;

        public Frm物料分类()
        {
            InitializeComponent();
            this.dockPanel3.Dock = DevExpress.XtraBars.Docking.DockingStyle.Fill;
            this.treeList1.DataSource = FunctionHelper.JsonDeserialization<List<DTO物料分类json>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialCatelogAll"));
            this.treeList1.ExpandAll();
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            this.dockPanel3.Text = this.treeList1.FocusedNode["catelogName"].ToString();
            this.catalogId = this.treeList1.FocusedNode["id"].ToString();
            this.list物料清单 = FunctionHelper.JsonDeserialization<List<DTO物料清单>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialData?catelogId=" + this.catalogId));
            if (this.list物料清单.Count > 0)
            {
                this.gridControl1.DataSource = this.list物料清单;
                this.DTO物料清单 = this.list物料清单[0];
                show当前行数据(this.DTO物料清单);
                不可修改();
            }
            else
            {
                this.gridControl1.DataSource = null;
                置空();
                不可修改();
            }
        }
        private void toolStripMenuItem新增分类_Click(object sender, EventArgs e)
        {
            string catelogId = "";
            try { catelogId = this.treeList1.FocusedNode["id"].ToString(); } catch { catelogId = null; }
            new Frm物料分类新增(catelogId).ShowDialog();
            this.treeList1.DataSource = FunctionHelper.JsonDeserialization<List<DTO物料分类json>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialCatelogAll"));
            this.treeList1.ExpandAll();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.DTO物料清单 = this.list物料清单[e.RowHandle];
            show当前行数据(this.DTO物料清单);
            不可修改();
        }
        private void show当前行数据(DTO物料清单 DTO物料清单)
        {
            this.textEdit名称.Text = DTO物料清单.material_name_cn;
            this.textEdit代码.Text = DTO物料清单.material_code;
            this.textEdit成分.Text = DTO物料清单.material_composition;
            this.textEdit幅宽.Text = DTO物料清单.material_spec;
            this.textEdit备注.Text = DTO物料清单.remarks;
        }
        private void 置空()
        {
            this.textEdit名称.Text = null;
            this.textEdit代码.Text = null;
            this.textEdit成分.Text = null;
            this.textEdit幅宽.Text = null;
            this.textEdit备注.Text = null;
        }
        private void 不可修改()
        {
            this.textEdit名称.ReadOnly = true;
            this.textEdit代码.ReadOnly = true;
            this.textEdit成分.ReadOnly = true;
            this.textEdit幅宽.ReadOnly = true;
            this.textEdit备注.ReadOnly = true;
        }

        private void simpleButton新增_Click(object sender, EventArgs e)
        {
            置空();
            this.textEdit名称.ReadOnly = false;
            this.textEdit代码.ReadOnly = false;
            this.textEdit成分.ReadOnly = false;
            this.textEdit幅宽.ReadOnly = false;
            this.textEdit备注.ReadOnly = false;
            this.DTO物料清单 = new DTO物料清单();
        }

        private void simpleButton修改_Click(object sender, EventArgs e)
        {
            this.textEdit名称.ReadOnly = false;
            this.textEdit代码.ReadOnly = false;
            this.textEdit成分.ReadOnly = false;
            this.textEdit幅宽.ReadOnly = false;
            this.textEdit备注.ReadOnly = false;
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textEdit名称.Text) || String.IsNullOrEmpty(this.textEdit代码.Text))
            {
                MessageBox.Show("名称及代码不能为空");
                return;
            }
            this.DTO物料清单.build(this.textEdit名称.Text, this.textEdit代码.Text, this.textEdit成分.Text, this.textEdit幅宽.Text, this.textEdit备注.Text);
            DTO物料清单 dto物料清单 = FunctionHelper.JsonDeserialization<DTO物料清单>(RestCall.PostHttp(URL.接口url + "/material/saveAccessoryMaterial?catelogId=" + this.catalogId + "&vender_id=" + "", FunctionHelper.JsonSerialization(this.DTO物料清单)));
            this.list物料清单 = FunctionHelper.JsonDeserialization<List<DTO物料清单>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialData?catelogId=" + this.catalogId));
            this.gridControl1.DataSource = this.list物料清单;
            show当前行数据(dto物料清单);
            不可修改();
        }

        private void simpleButton导入Excel_Click(object   sender, EventArgs e)
        {
            DataSet result = ExcelUtil.getExcelDatas();
            if (result == null)
            {
                return;
            }
            DataTable dataTable = result.Tables[0];
            List<DTO物料清单> list = new List<DTO物料清单>();
            foreach (DataRow dataRow in dataTable.Rows)
            {
                list.Add(new DTO物料清单(dataRow));
            }
            new Frm导入预览(list, this.catalogId,this.list物料清单).ShowDialog();
            this.list物料清单 = FunctionHelper.JsonDeserialization<List<DTO物料清单>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialData?catelogId=" + this.catalogId));
            this.gridControl1.DataSource = this.list物料清单;
        }

        private void simpleButton删除_Click(object sender, EventArgs e)
        {
            if (this.DTO物料清单 == null)
            {
                return;
            }
            RestCall.PostHttp(URL.接口url + "/material/deleteAccessoryMaterialRalation?catelogId=" + this.catalogId + "&materialId=" + this.DTO物料清单.material_id, "");
            this.list物料清单.Remove(this.DTO物料清单);
            this.gridControl1.DataSource = this.list物料清单;
            this.gridView1.RefreshData();
        }

        private void simpleButton导出_Click(object sender, EventArgs e)
        {
            //SaveFileDialog fileDialog = new SaveFileDialog();
            //fileDialog.Title = "导出Excel";
            //fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            //DialogResult dialogResult = fileDialog.ShowDialog(this);
            //if (dialogResult == DialogResult.OK)
            //{
            //    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
            //    DataTable data = (DataTable)this.gridControl1.DataSource;
            //    this.gridControl1.ExportToXls(fileDialog.FileName);
            //    DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        }
    }

    public class DTO物料清单
    {
        public string create_date { get; set; }
        public string delete_flag { get; set; }
        public string material_category { get; set; }
        public string material_code { get; set; }
        public string material_color { get; set; }
        public string material_composition { get; set; }
        public string material_gram_weight { get; set; }
        public string material_id { get; set; }
        public string material_name_cn { get; set; }
        public string material_other1 { get; set; }
        public string material_other3 { get; set; }
        public string material_other4 { get; set; }
        public string material_sample { get; set; }
        public string material_season { get; set; }
        public string material_source { get; set; }
        public string material_spec { get; set; }
        public string material_spec_unit_cd { get; set; }
        public string material_style { get; set; }
        public string material_thread_count { get; set; }
        public string material_unit_cd { get; set; }
        public string material_weave { get; set; }
        public string material_year { get; set; }
        public string remarks { get; set; }
        public DTO物料清单(string create_date, string delete_flag, string material_category, string material_code, string material_color, string material_composition, string material_gram_weight, string material_id, string material_name_cn, string material_other1, string material_other3, string material_other4, string material_sample, string material_season, string material_source, string material_spec, string material_spec_unit_cd, string material_style, string material_thread_count, string material_unit_cd, string material_weave, string material_year, string remarks)
        {
            this.create_date = create_date;
            this.delete_flag = delete_flag;
            this.material_category = material_category;
            this.material_code = material_code;
            this.material_color = material_color;
            this.material_composition = material_composition;
            this.material_gram_weight = material_gram_weight;
            this.material_id = material_id;
            this.material_name_cn = material_name_cn;
            this.material_other1 = material_other1;
            this.material_other3 = material_other3;
            this.material_other4 = material_other4;
            this.material_sample = material_sample;
            this.material_season = material_season;
            this.material_source = material_source;
            this.material_spec = material_spec;
            this.material_spec_unit_cd = material_spec_unit_cd;
            this.material_style = material_style;
            this.material_thread_count = material_thread_count;
            this.material_unit_cd = material_unit_cd;
            this.material_weave = material_weave;
            this.material_year = material_year;
            this.remarks = remarks;
        }

        public DTO物料清单(DataRow dataRow)
        {
            try
            {
                this.material_code = dataRow["column0"].ToString();
                this.material_name_cn = dataRow["column1"].ToString();
                this.material_composition = dataRow["column2"].ToString();
                this.material_spec = dataRow["column3"].ToString();
            }
            catch { }
        }
        public DTO物料清单(DTO物料清单 dto)
        {
            this.material_id = dto.material_id;
            this.material_code = dto.material_code;
            this.material_name_cn = dto.material_name_cn;
            this.material_composition = dto.material_composition;
            this.material_spec = dto.material_spec;
        }

        public DTO物料清单()
        {
        }

        /// <summary>
        /// 页面传参
        /// </summary>
        /// <param name="material_name_cn"></param>
        /// <param name="material_code"></param>
        /// <param name="material_composition"></param>
        /// <param name="material_spec"></param>
        /// <param name="remarks"></param>
        public void build(string material_name_cn, string material_code, string material_composition, string material_spec, string remarks)
        {
            this.material_name_cn = material_name_cn;
            this.material_code = material_code;
            this.material_composition = material_composition;
            this.material_spec = material_spec;
            this.remarks = remarks;
        }

        /// <summary>
        /// 修改保存
        /// </summary>
        /// <param name="dTO物料清单"></param>
        public void build(DTO物料清单 dTO物料清单)
        {
            this.material_name_cn = dTO物料清单.material_name_cn;
            this.material_code = dTO物料清单.material_code;
            this.material_composition = dTO物料清单.material_composition;
            this.material_spec = dTO物料清单.material_spec;
            this.remarks = dTO物料清单.remarks;
        }
    }
}